package com.leon.module.action;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.leon.classify.domain.Area;
import com.leon.classify.domain.FirstClass;
import com.leon.classify.domain.LeftRecommon;
import com.leon.classify.domain.MiddleArea;
import com.leon.classify.domain.RightArea;
import com.leon.classify.domain.SecondClass;
import com.leon.classify.service.ClassifyService;
import com.leon.common.action.BaseAction;
import com.leon.common.util.VVMappingUtil;
import com.leon.module.service.ModuleService;

public class ModuleAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ModuleService moduleService;
	private ClassifyService classifyService;
	private final static String SUCCESSMSG="{success:true}";
	
	public ModuleService getModuleService() {
		return moduleService;
	}
	public void setModuleService(ModuleService moduleService) {
		this.moduleService = moduleService;
	}
	public ClassifyService getClassifyService() {
		return classifyService;
	}
	public void setClassifyService(ClassifyService classifyService) {
		this.classifyService = classifyService;
	}
	/**
	 * 创建一个area
	 */
	public void createArea(){
		Area area = null;
		
		String areaId=this.getRequest().getParameter("areaId");
		
		String areaName=this.getRequest().getParameter("areaName");
		String areaDesc=this.getRequest().getParameter("areaDesc");
		String displayOrder=this.getRequest().getParameter("displayOrder");
		int state=Integer.valueOf(this.getRequest().getParameter("state"));
		if(displayOrder.equals("")){
			displayOrder="1";
		}
		if(areaId.equals("") || areaId==""){
			area=new Area();
			area.setAreaDesc(areaDesc);
			area.setAreaName(areaName);
			area.setDisplayOrder(Integer.valueOf(displayOrder));
			area.setState(state);
			moduleService.createArea(area);
		}else{
			int location=Integer.valueOf(this.getRequest().getParameter("location"));
			area=moduleService.getAreaById(areaId);
			if(location==1){
				LeftRecommon lr=new LeftRecommon();
				lr.setArea(area);
				lr.setAreaName(areaName);
				lr.setAreaDesc(areaDesc);
				lr.setDisplayOrder(Integer.valueOf(displayOrder));
				lr.setState(state);
				moduleService.createLeftRe(lr);
			}else if(location==2){
				MiddleArea ma=new MiddleArea();
				ma.setArea(area);
				ma.setAreaName(areaName);
				ma.setAreaDesc(areaDesc);
				ma.setDisplayOrder(Integer.valueOf(displayOrder));
				ma.setState(state);
				moduleService.createMiddleArea(ma);
			}else if(location==3){
				RightArea ra=new RightArea();
				ra.setArea(area);
				ra.setAreaName(areaName);
				ra.setAreaDesc(areaDesc);
				ra.setDisplayOrder(Integer.valueOf(displayOrder));
				ra.setState(state);
				moduleService.createRightArea(ra);
			}
		}
		
		this.sendMessage(SUCCESSMSG);
	}
	/**
	 * 使用response发送数据到HTML
	 * @param str
	 */
	public void sendMessage(String str){
		try {
			this.getResponse().getWriter().write(str);
			this.getResponse().getWriter().flush();
			this.getResponse().getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 得到所有的模块树
	 */
	public void getAllArea(){
		List<Area>list=moduleService.getAllArea();
		StringBuffer str=new StringBuffer("[");
		if(list.size()>0 && list!=null){
			
			for(Area a:list){
				StringBuffer mvalues=new StringBuffer("");
				mvalues.append("mvalues:[");
				str.append("{text:'"+a.getAreaName()+"',id:'"+a.getAreaId()+"',type:'area',children:[");
				Set <LeftRecommon> lrs=a.getLeftRecommons();
				int i=0;
				if(lrs.size()>0 && lrs!=null){
					i++;
					mvalues.append("1,");
					LeftRecommon lr=((LeftRecommon) lrs.toArray()[0]);
					str.append("{text:'"+lr.getAreaName()+"(左)',id:'"+lr.getLeftRecommonId()+"',type:'left'");
					Set <FirstClass> fcs=lr.getFirstClasses();
					if(fcs!=null && fcs.size()>0){
						str.append(",children:[");
						for(FirstClass fc:fcs){
							str.append("{text:'"+fc.getClassName()+"',id:'"+fc.getFirstClassId()+"',type:'first',leaf:true},");
						}
						str.deleteCharAt(str.length()-1);
						str.append("]},");
					}else{
						str.append(",leaf:true},");
					}
				}
				Set<MiddleArea> mas=a.getMiddleAreas();
				if(mas.size()>0 && mas!=null){
					i++;
					
					//MiddleArea ma=(MiddleArea)mas.toArray()[0];
					for(MiddleArea ma:mas){
						mvalues.append("2,");
						str.append("{text:'"+ma.getAreaName()+"(中)',id:'"+ma.getMiddleAreaId()+"',type:'middle'");
						Set <SecondClass>scs=ma.getSecondClasses();
						if(scs.size()>0 && scs!=null){
							str.append(",children:[");
							for(SecondClass sc:scs){
								str.append("{text:'"+sc.getClassName()+"',id:'"+sc.getSecClassId()+"',type:'second',leaf:true},");
							}
							str.deleteCharAt(str.length()-1);
							str.append("]},");
						}else{
							str.append(",leaf:true},");
						}
					}
					
				}
				Set <RightArea>ras=a.getRightAreas();
				if(ras.size()>0 && ras!=null){
					i++;
					//RightArea ra=(RightArea)ras.toArray()[0];
					for(RightArea ra:ras){
						mvalues.append("3,");
						str.append("{text:'"+ra.getAreaName()+"(右)',id:'"+ra.getRightAreaId()+"',type:'right'");
						Set <SecondClass> scs=ra.getSecondClasses();
						if(scs!=null && scs.size()>0){
							str.append(",children:[");
							for(SecondClass sc:scs){
								str.append("{text:'"+sc.getClassName()+"',id:'"+sc.getSecClassId()+"',type:'second',leaf:true},");
							}
							str.deleteCharAt(str.length()-1);
							str.append("]},");
						}else{
							str.append(",leaf:true},");
						}
					}
				}
				if(i>0){
					str.deleteCharAt(str.length()-1);
				}
				if(mvalues.charAt(mvalues.length()-1)==','){
					mvalues.deleteCharAt(mvalues.length()-1);
				}
				mvalues.append("]");
				str.append("],"+mvalues+"},");
			}
			str.deleteCharAt(str.length()-1);
		}
		str.append("]");
		this.sendMessage(str.toString());
	}
	/**
	 * 载入一个模块
	 */
	public void loadArea(){
		String areaId=this.getRequest().getParameter("areaId");
		Area area=moduleService.getAreaById(areaId);
		if(area!=null){
			area.setLeftRecommons(null);
			area.setRightAreas(null);
			area.setMiddleAreas(null);
			String str=VVMappingUtil.object2json(area);
			this.sendMessage("{success: true,data:["+str+"]}");
		}
	}
	/**
	 * 更新一个Area
	 */
	public void updateArea(){
		String areaId=this.getRequest().getParameter("areaId");
		Area area=moduleService.getAreaById(areaId);
		
		String areaName=this.getRequest().getParameter("areaName");
		String areaDesc=this.getRequest().getParameter("areaDesc");
		String displayOrder=this.getRequest().getParameter("displayOrder");
		int state=Integer.valueOf(this.getRequest().getParameter("state"));
		if(displayOrder.equals("")){
			displayOrder="1";
		}
		area.setAreaDesc(areaDesc);
		area.setAreaName(areaName);
		area.setDisplayOrder(Integer.valueOf(displayOrder));
		area.setState(state);
		moduleService.createArea(area);
		this.sendMessage(SUCCESSMSG);
	}
	/**
	 * 载入一个left
	 */
	public void loadLeftRec(){
		String leftRecommonId=this.getRequest().getParameter("leftRecommonId");
		LeftRecommon lr=moduleService.getLeftRecById(leftRecommonId);
		StringBuffer str=new StringBuffer("{success:true,data:[");
		if(lr!=null){
			str.append("{leftRecommonId:'"+lr.getLeftRecommonId()+"',areaName:'"+lr.getAreaName()+"',areaDesc:'"+lr.getAreaDesc()+"',state:'"+lr.getState()+"',displayOrder:'"+lr.getDisplayOrder()+"'}]}");
			this.sendMessage(str.toString());
		}
	}
	/**
	 * 更新一个左列推荐
	 */
	public void updateLeftRec(){
		String leftRecommonId=this.getRequest().getParameter("leftRecommonId");
		String itemselector=this.getRequest().getParameter("itemselector");//已选择的firstclass列表
		LeftRecommon lr=moduleService.getLeftRecById(leftRecommonId);
		String selected[]=null;
		
		Set <FirstClass> list=lr.getFirstClasses();
		Set <FirstClass> olist=new HashSet<FirstClass>();
		if(list!=null && list.size()>0){//先删除以前的
			for(FirstClass fc:list){
				fc.setLeftRecommon(null);
				olist.add(fc);
			}
			lr.setFirstClasses(olist);
			moduleService.createLeftRe(lr);
		}
		
		if(!itemselector.equals("")){
			selected=itemselector.split(",");
			Set<FirstClass> fcs=new HashSet<FirstClass>();
			for(int i=0;i<selected.length;i++){
				FirstClass fc=classifyService.getFirstClass(selected[i]);
				fc.setLeftRecommon(lr);
				fcs.add(fc);
			}
			lr.setFirstClasses(fcs);//加入已选择的firstclass
		}
		String areaName=this.getRequest().getParameter("areaName");
		String areaDesc=this.getRequest().getParameter("areaDesc");
		String displayOrder=this.getRequest().getParameter("displayOrder");
		int state=Integer.valueOf(this.getRequest().getParameter("state"));
		if(displayOrder.equals("")){
			displayOrder="1";
		}
		lr.setAreaName(areaName);
		lr.setAreaDesc(areaDesc);
		lr.setDisplayOrder(Integer.valueOf(displayOrder));
		lr.setState(state);
		moduleService.createLeftRe(lr);
		this.sendMessage(SUCCESSMSG);
	}
	/**
	 * 载入一个middle
	 */
	public void loadMiddleArea(){
		String middleAreaId=this.getRequest().getParameter("middleAreaId");
		MiddleArea ma=moduleService.getMiddleArea(middleAreaId);
		StringBuffer str=new StringBuffer("{success:true,data:[");
		if(ma!=null){
			str.append("{middleAreaId:'"+ma.getMiddleAreaId()+"',areaName:'"+ma.getAreaName()+"',areaDesc:'"+ma.getAreaDesc()+"',state:'"+ma.getState()+"',displayOrder:'"+ma.getDisplayOrder()+"'}]}");
			this.sendMessage(str.toString());
		}
	}
	/**
	 * 修改中间模块
	 */
	public void updateMiddleArea(){
		String middleAreaId=this.getRequest().getParameter("middleAreaId");
		String itemselector=this.getRequest().getParameter("itemselector");//已选择的firstclass列表
		MiddleArea ma=moduleService.getMiddleArea(middleAreaId);
		String selected[]=null;
		
		Set <SecondClass> list=ma.getSecondClasses();
		Set <SecondClass> olist=new HashSet<SecondClass>();
		if(list!=null && list.size()>0){//先删除以前的
			for(SecondClass sc:list){
				sc.setMiddleArea(null);
				olist.add(sc);
			}
			ma.setSecondClasses(olist);
			moduleService.createMiddleArea(ma);
		}
		
		if(!itemselector.equals("")){
			selected=itemselector.split(",");
			Set<SecondClass> scs=new HashSet<SecondClass>();
			for(int i=0;i<selected.length;i++){
				SecondClass sc=classifyService.getSecondClass(selected[i]);
				sc.setMiddleArea(ma);
				scs.add(sc);
			}
			ma.setSecondClasses(scs);//加入已选择的firstclass
		}
		
		String areaName=this.getRequest().getParameter("areaName");
		String areaDesc=this.getRequest().getParameter("areaDesc");
		String displayOrder=this.getRequest().getParameter("displayOrder");
		int state=Integer.valueOf(this.getRequest().getParameter("state"));
		if(displayOrder.equals("")){
			displayOrder="1";
		}
		ma.setAreaName(areaName);
		ma.setAreaDesc(areaDesc);
		ma.setDisplayOrder(Integer.valueOf(displayOrder));
		ma.setState(state);
		moduleService.createMiddleArea(ma);
		this.sendMessage(SUCCESSMSG);
	}
	/**
	 * 载入一个RightArea
	 */
	public void loadRightArea(){
		String rightAreaId=this.getRequest().getParameter("rightAreaId");
		RightArea ra=moduleService.getRightAreaById(rightAreaId);
		StringBuffer str=new StringBuffer("{success:true,data:[");
		if(ra!=null){
			str.append("{rightAreaId:'"+ra.getRightAreaId()+"',areaName:'"+ra.getAreaName()+"',areaDesc:'"+ra.getAreaDesc()+"',state:'"+ra.getState()+"',displayOrder:'"+ra.getDisplayOrder()+"'}]}");
			this.sendMessage(str.toString());
		}
	}
	/**
	 * 更新一个RightArea
	 */
	public void updateRightArea(){
		String rightAreaId=this.getRequest().getParameter("rightAreaId");
		RightArea ra=moduleService.getRightAreaById(rightAreaId);
		String itemselector=this.getRequest().getParameter("itemselector");//已选择的firstclass列表
		String selected[]=null;
		
		Set <SecondClass> list=ra.getSecondClasses();
		Set <SecondClass> olist=new HashSet<SecondClass>();
		if(list!=null && list.size()>0){//先删除以前的
			for(SecondClass sc:list){
				sc.setRightArea(null);
				olist.add(sc);
			}
			ra.setSecondClasses(olist);
			moduleService.createRightArea(ra);
		}
		
		if(!itemselector.equals("")){
			selected=itemselector.split(",");
			Set<SecondClass> scs=new HashSet<SecondClass>();
			for(int i=0;i<selected.length;i++){
				SecondClass sc=classifyService.getSecondClass(selected[i]);
				sc.setRightArea(ra);
				scs.add(sc);
			}
			ra.setSecondClasses(scs);//加入已选择的firstclass
		}
		
		String areaName=this.getRequest().getParameter("areaName");
		String areaDesc=this.getRequest().getParameter("areaDesc");
		String displayOrder=this.getRequest().getParameter("displayOrder");
		int state=Integer.valueOf(this.getRequest().getParameter("state"));
		if(displayOrder.equals("")){
			displayOrder="1";
		}
		
		ra.setAreaName(areaName);
		ra.setAreaDesc(areaDesc);
		ra.setDisplayOrder(Integer.valueOf(displayOrder));
		ra.setState(state);
		moduleService.createRightArea(ra);
		this.sendMessage(SUCCESSMSG);
	}
	public void delLeftRecById(){
		String leftRecommonId=this.getRequest().getParameter("leftRecommonId");
		moduleService.delLeftRecById(leftRecommonId);
	}
	
	public void delMiddleAreaById(){
		String middleAreaId=this.getRequest().getParameter("middleAreaId");
		moduleService.delMiddleAreaById(middleAreaId);
	}
	
	public void delRightAreaById(){
		String rightAreaId=this.getRequest().getParameter("rightAreaId");
		moduleService.delRightAreaById(rightAreaId);
	}
	
	public void delAreaById(){
		String areaId=this.getRequest().getParameter("areaId");
		moduleService.delAreaById(areaId);
	}
}
